package adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.qianfeng.lingshijia.R;

import java.util.ArrayList;
import java.util.List;

import custom.Commodity;
import custom.Commodity_totalList;
import entity.HeadEntity;
import entity.TotalListEntity;

/**
 * Created by Mr.xingkong on 2016/1/11.
 */
public class TotalListAdapter extends BaseAdapter implements View.OnClickListener {
    List<TotalListEntity.DataEntity.PListEntity> data;
    Context context;
    String type = "http://cmsproxy.haolingzui.com/todayPromotionProdcuts.do";
    public TotalListAdapter(Context context) {
        this.context = context;
        data = new ArrayList<>();
    }

    /**
     * 设置数据
     *
     * @param data
     */
    public void setData(List<TotalListEntity.DataEntity.PListEntity> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    public void addData(List<TotalListEntity.DataEntity.PListEntity> data){
        this.data.addAll(data);
        notifyDataSetChanged();
    }

    public void clearData(){
        data.clear();
    }

    public void setType(String type)
    {
        this.type = type;//使用URL区分数据
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    String picUrl, name, price, message;

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = new Commodity_totalList(context);
            viewHolder = new ViewHolder();
            viewHolder.commodity = (Commodity_totalList) convertView;
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        picUrl = data.get(position).getImgUrl();
        name = data.get(position).getTitle();
        price = "¥" + data.get(position).getPriceStr();

        //message根据数据不同而不同
        TextView message_tv = viewHolder.commodity.getMessage();
        //以下是特惠马上抢处理
        if ("http://cmsproxy.haolingzui.com/todayPromotionProdcuts.do".equals(type))
        {
            float youhui =(float) (data.get(position).getOriginalPrice() - data.get(position).getPrice())/100;
            String message = " "+youhui + "元";
            message_tv.setText(message);
            Drawable sheng_icon= context.getResources().getDrawable(R.drawable.sheng_icon);
            sheng_icon.setBounds(0, 0, sheng_icon.getIntrinsicWidth(), sheng_icon.getIntrinsicHeight());
            message_tv.setCompoundDrawables(sheng_icon, null,null, null);
        }
        //以下囤货必备
        if ("http://cmsproxy.haolingzui.com/stockpileProducts.do".equals(type))
        {
            String message = "¥"+data.get(position).getOriginalPriceStr();
            message_tv.setText(message);
        }
        //热销单品
        if ("http://cmsproxy.haolingzui.com/hotSaleProdcuts.do".equals(type))
        {
            String message = data.get(position).getSalesVolume()+"人购买";
            message_tv.setText(message);
        }
        convertView.setOnClickListener(this);
        viewHolder.commodity.shoppingCart_iv_total.setOnClickListener(this);

        viewHolder.commodity.setData(picUrl, name, price);
        return convertView;
    }

    @Override
    public void onClick(View v)
    {
        if (v.getId() == R.id.shoppingCart_iv_total)
        {
            Toast.makeText(context, "放入购物车", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(context, "跳转", Toast.LENGTH_SHORT).show();
        }
    }

    class ViewHolder
    {
        Commodity_totalList commodity;
    }

}
